/*
 * Copyright:  2018 smarabbit studio.
 *
 *  Licensed under the Confluent Community License; you may not use this file
 *  except in compliance with the License.  You may obtain a copy of the License at
 *
 *  http://www.confluent.io/confluent-community-license
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 *  WARRANTIES OF ANY KIND, either express or implied.  See the License for the
 *  specific language governing permissions and limitations under the License.
 *
 *  @作   者： 黄开晖 (117227773@qq.com)
 *  @日   期:  2021年5月28日
 */
package com.massyframework.beanskin.test;

import java.util.function.Consumer;
import java.util.function.Supplier;

import com.massyframework.beanskin.Module;
import com.massyframework.beanskin.ModuleIdentifier;
import com.massyframework.beanskin.ModuleNotFoundException;
import com.massyframework.beanskin.SimpleModuleIdentifier;
import com.massyframework.beanskin.assembly.Assembly;
import com.massyframework.beanskin.framework.Framework;
import com.massyframework.beanskin.launcher.DefalutLauncher;
import com.massyframework.beanskin.launching.Launcher;
import com.massyframework.beanskin.util.exceptioncollect.ExceptionCollectUtils;
import com.massyframework.beanskin.util.exceptioncollect.ExceptionGroup;
import com.massyframework.beanskin.util.version.Version;

/**
 * 运行框架工具类
 * @author huangkh
 *
 */
public abstract class FrameworkUtils {

	private static Framework INSTANCE;

	/**
	 * 启动运行框架
	 * @return {@link Framework}
	 */
	public static Framework launch() throws Exception {
		return launch(null);
	}
	
	/**
	 * 启动运行框架 
	 * @param provider {@link Supplier},配置参数提供者
	 * @return
	 * @throws Exception
	 */
	public static Framework launch(Consumer<ConfigurationBuilder> action) 
			throws Exception{		
		if (INSTANCE == null) {
			synchronized(FrameworkUtils.class) {
				ConfigurationBuilder builder = new ConfigurationBuilder();
				if (action != null) {
					action.accept(builder);
				}
								
				ExceptionGroup group = new ExceptionGroup();
				ExceptionCollectUtils.bind(group);
				
				Launcher launcher = new DefalutLauncher();
				INSTANCE =  launcher.launch(builder.build());
				INSTANCE.init();
				
			}
		}
		
		return INSTANCE;
	}
	
	/**
	 * 按{@code identifier}查找模块
	 * @param identifier {@link ModuleIdentifier}
	 * @return {@link Module}
	 */
	protected static <T extends Module<?>> T findModule(ModuleIdentifier identifier, Class<T> moduleType){
		if (INSTANCE == null) return null;
		return INSTANCE.findModuleByIdentifier(identifier, moduleType);
	}
	
	/**
	 * 获取Framework实例
	 * @return {@link Framework}
	 */
	public static Framework getInstance() {
		return INSTANCE;
	}
	
	/**
	 * 按{@code identifier}查找装配件
	 * @param identifier {@link ModuleIdentifier}
	 * @return {@link Assembly}
	 */
	public static Assembly findAssembly(ModuleIdentifier identifier) {
		return findModule(identifier,Assembly.class);
	}
	
	/**
	 * 查找装配件
	 * @param groupId {@link String},组编号
	 * @param moduleId {@link String},模块编号
	 * @param version {@link Version},版本
	 * @return {@link Assembly}
	 */
	public static Assembly findAssembly(String groupId, String moduleId, Version version) {
		ModuleIdentifier identifier =
				SimpleModuleIdentifier.newBuilder()
					.groupId(groupId)
					.moduleId(moduleId)
					.version(version)
					.build();
		return findModule(identifier, Assembly.class);
	}
	
	public static Assembly awaitWorking(ModuleIdentifier identifier, int interval) {
		Assembly assembly = findAssembly(identifier);
		if (assembly == null) {
			throw new ModuleNotFoundException(identifier);
		}
		
		while (!assembly.isWorking()) {
			try {
				System.out.println("wait "+ assembly+ " working...");
				Thread.sleep(interval);
			}catch(Exception e) {
				
			}
		}
		
		return assembly;
	}
}
